package com.cgly.medical.controller;

import com.alibaba.excel.EasyExcel;
import com.cgly.medical.annotation.WxlRateLimiter;
import com.cgly.medical.domain.Account;
import com.cgly.medical.domain.Doctor;
import com.cgly.medical.domain.DoctorLevel;
import com.cgly.medical.domain.TreatType;
import com.cgly.medical.model.PatientModel;
import com.cgly.medical.service.AppointmentTimeService;
import com.cgly.medical.service.DoctorLevelService;
import com.cgly.medical.service.DoctorService;
import com.cgly.medical.service.TreatTypeService;
import com.cgly.medical.util.Msg;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangxiangli
 * @create 2022-08-16-16:11
 */
@Api(description = "医师管理")
@RestController
@RequestMapping("/doctorservice")
@CrossOrigin  //解决跨域问题
public class DoctorController {

    @Autowired
    private DoctorService doctorService;


    @Autowired
    private TreatTypeService treatTypeService;

    @Autowired
    private DoctorLevelService doctorLevelService;


    @Autowired
    private AppointmentTimeService appointmentTimeService;

    @Autowired
    private RedisTemplate redisTemplate;

    //分页查询
    @Cacheable(value = "Doctor", key = "'selectAllDoctor'")
    @PostMapping("selectAllDoctor/{current}/{limit}")
    public Msg selectAllDoctor(@PathVariable int current,
                               @PathVariable int limit,
                               @RequestBody(required = false) Doctor doctor) {

//        PageHelper.startPage(current, limit);
        List<Doctor> doctors = doctorService.selectAll(doctor);
//  PageInfo<Doctor> doctorPageInfo=new PageInfo<Doctor>(doctors,limit);
//        PageInfo<Doctor> doctorPageInfo1 = new PageInfo<Doctor>(doctors, limit);
//        List<Doctor> list = doctorPageInfo1.getList();
        List<Doctor> subList = doctors.stream().skip((current-1)*limit).limit(limit).
                collect(Collectors.toList());
        long total = doctors.size();
        if (doctors == null) {
            return Msg.fail();
        }
        return Msg.success().data("total", total).data("rows", subList);
    }

    //通过id进行查询
    @GetMapping("selectDoctorByid/{id}")
    public Msg selectDoctorByid(@PathVariable String id) {
        Doctor doctor = doctorService.selectDoctorByid(id);
        if (doctor == null) {
            return Msg.fail();
        }
        return Msg.success().data("rows", doctor);
    }


    //修改方法
    @PostMapping("updateDoctorinfo")
    public Msg updateDoctorinfo(@RequestBody Doctor doctor) {
        int i = doctorService.updateDoctorInfo(doctor);
        if (i == 0) {
            return Msg.fail();
        }
        return Msg.success();
    }
    //通过id进行查询
    @GetMapping("/getDoctorId/{username}")
    public Msg getDoctorId(@PathVariable String username) {
        Long doctorId = doctorService.getDoctorId(username);
        if (doctorId == null) {
            return Msg.fail();
        }
        return Msg.success().data("doctorId", doctorId);
    }


    //删除方法
    @DeleteMapping("deleteDoctorinfo/{id}")
    public Msg deleteDoctorinfo(@PathVariable String id) {
        int i = doctorService.deleteDoctorinfo(id);
        if (i == 0) {
            return Msg.fail();
        }
        return Msg.success();
    }


    @PostMapping("addDoctorinfo")
    public Msg addDoctorinfo(@RequestBody Doctor doctor) {
        String phone = doctor.getPhone();
// Account account = accountService.securityLogin(phone);
        Account account = doctorService.selectAccountByPhone(phone);
        if (account == null) {
            return Msg.fail();
        }
        Long id = account.getId();
        Integer acid = id.intValue();
        doctor.setAccountId(acid);
        int i = doctorService.addDoctorinfo(doctor);
        Doctor Newdoctor = doctorService.selectDoctorByPhone(phone);
        appointmentTimeService.addAppointmentTime(Newdoctor.getId());
        return Msg.success();

}

@WxlRateLimiter(rate = 1,timeOut = 500)
@GetMapping("selectAllTreatTypeAnddoctorLevel")
 public Msg selectAllTreatTypeAnddoctorLevel(){
 List<TreatType> treatTypes = treatTypeService.selectAllTreatType();
 List<DoctorLevel> doctorLevels = doctorLevelService.selectAllDoctorLevel();
 List<Doctor> doctorList = doctorService.selectAll(new Doctor());
 return Msg.success().data("rows",treatTypes).data("doctorLevels",doctorLevels).data("doctorList",doctorList);
}


 //批量导入添加医师方法
 @PostMapping("/BatchaddDoctors")
 public Msg BatchaddDoctors(MultipartFile file){

  doctorService.BatchaddDoctors(file,doctorService);
  return Msg.success();
 }


 //导出医师数据为Excel

 @RequestMapping("exportDoctorInfo")
 public Msg exportDoctorInfo(){
  List<Doctor> doctors = doctorService.selectAll(new Doctor());
  String fileName = "F:/" + "DoctorInfo" + System.currentTimeMillis() + ".xlsx";
  // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
  EasyExcel.write(fileName, Doctor.class).sheet("模板").doWrite(doctors);
  return Msg.success().data("doctors",doctors);
 }

 @GetMapping("selectByTypeId/{typeId}")
 public Msg selectByTypeId(@PathVariable("typeId") Integer typeId){
  List<Doctor> doctorList = doctorService.selectByTypeId(typeId);
  return Msg.success().data("doctorList",doctorList);
 }
}
